package com.application.module_chat2.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.application.module_chat2.R;
import com.application.module_chat2.adapter.MessageAdapter;
import com.application.module_chat2.bean.uibean.MessageBean;
import com.application.module_chat2.presenter.MessagePresenter;
import com.application.module_chat2.view.MessageView;
import com.application.mylibrary.widget.RefreshView;
import com.application.mylibrary.base.mvp.BaseMvpFragment;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import io.rong.imlib.model.Message;

/**
 * Created by lixuce on 2019/8/9.
 */
public class FragmentMessage extends BaseMvpFragment<MessageView, MessagePresenter>
        implements MessageView, RefreshView.RefreshListener, BaseQuickAdapter.OnItemClickListener {

    private RefreshView refreshView;
    private MessageAdapter adapter;

    @Override
    public MessageView createView() {
        return this;
    }

    @Override
    public MessagePresenter createPresenter() {
        return new MessagePresenter();
    }


    @Override
    protected void beforeInit(Bundle arguments) {

    }

    @Override
    public int getLayoutId() {
        return R.layout.fragment_message;
    }

    @Override
    public void initView(View view) {
        refreshView = view.findViewById(R.id.re_message_view);
        RecyclerView listView = view.findViewById(R.id.rv_im_message);
        refreshView.setRefreshListener(this);
        listView.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new MessageAdapter(R.layout.item_message_chat);
        listView.setAdapter(adapter);
        adapter.setOnItemClickListener(this);
        loadData();
    }

    @Override
    public void addConversationList(List<MessageBean> beans) {
        adapter.setNewData(beans);
        if (refreshView != null) {
            if (refreshView.isRefreshing()) {
                refreshView.setFinish();
            }
        }
    }

    @Override
    public void addConversation(MessageBean bean) {

    }

    @Override
    public void upConversation(MessageBean bean) {

    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void receiverMessage(Message message){
        if (getPresenter()!=null) {
            getPresenter().handleMessage(message);
        }
    }

    @Override
    public void onRefresh() {
        loadData();
    }

    private void loadData() {
        if (getPresenter() != null) {
            getPresenter().loadConversation();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

    }
}
